'use strict';

angular.module('hanxinBroker').controller('ModifyPwdCtrl', function (ngDialog, $http, $state) {
  var vm = this;
  vm.form = {};
  vm.form.mobile = undefined;
  $http.get('broker/api/userMobile').success(function (data) {
    vm.form.mobile = data.mobile;
  });


  vm.checkNewPwd = function checkNewPwd() {
    if (!vm.form.newPwd || !vm.form.confirmPwd) {
      vm.form.modifyPwdMessage = "请输入8位以上密码，且使用数字与字母组合";
    } else if (vm.form.newPwd != vm.form.confirmPwd) {
      vm.form.modifyPwdMessage = "两次输入的密码不一样";
    } else if (vm.form.newPwd == vm.form.password) {
      vm.form.modifyPwdMessage = "新密码不能与原密码一样";
    } else {
      vm.form.modifyPwdMessage = undefined;
    }
  };

  vm.checkPassword = function checkPassword() {
    if (!vm.form.password) {
      vm.form.passwordMessage = "请输入原密码";
    } else {
      vm.form.passwordMessage = undefined;
    }
  };
  vm.modifyPwd = function modifyPwd() {
    $http.post('broker/api/modifyPwd', {
      "mobile": vm.form.mobile,
      "password": vm.form.password,
      "newPwd": vm.form.newPwd,
      "confirmPwd": vm.form.confirmPwd
    }).success(function (data) {
      $state.go("userHome");
    }).error(function (data) {
      if (data.message == 'ERROR_PASSWORD') {
        vm.form.passwordMessage = "原密码输入错误，请重新输入";
      }
    });


  };


});
